﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ConfigReadWebApp.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {


        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;
        private readonly IConfiguration _configuration;
        private readonly StudentOptions _studentOptions;

        public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration configuration, IOptions<StudentOptions> options)
        {
            _logger = logger;
            _configuration = configuration;
            _studentOptions = options.Value;
            _logger.LogInformation("执行WeatherForecastController构造函数");
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            _logger.LogInformation("执行WeatherForecastController  Get");
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }

        [HttpGet]
        [Route("GetConfig")]
        public string GetConfig()
        {
            return $"SchoolName：{_configuration["SchoolName"]}\n" +
                $"ClassName：{_configuration["Student:ClassName"]}\n" +
                $"StudentList[0]:{_configuration["Student:StudentList:0"]}\n" +
                $"StudentList[1]:{_configuration["Student:StudentList:1"]}\n"+
                $"StudentList[2]:{_configuration["Student:StudentList:2"]}\n";
        }

        [HttpGet]
        [Route("GetConfigByEntity")]
        public string GetConfigByEntity()
        {
            return JsonConvert.SerializeObject(_studentOptions);
        }

    }
}
